"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】ddl_command_end触发器执行出错后，DDL命令效果被回滚
Create At   :   2024/07/20
Owner       :   sungang14
Description :
    预置条件：
        1.创建表
        create table t_eventtrigger_0065(id int);
        2.创建事件触发器函数：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0065()
        RETURNS event_trigger
        LANGUAGE plpgsql
        AS $$ BEGIN
        RAISE EXCEPTION 'command % is disabled', tg_tag;
        END;
        $$;
    测试步骤：
        1.创建事件触发器
        CREATE EVENT TRIGGER eventtri_eventtrigger_0065 ON ddl_command_end EXECUTE PROCEDURE f_eventtrigger_0065();
        2.执行DDL删除表
        drop table t_eventtrigger_0065;
        3.查询表
        select * from t_eventtrigger_0065;
Expect      : 
        1.创建事件触发器成功
        2.删除表触发执行函数f_eventtrigger_0065()
        command DROP TABLE is disabled
        3.函数异常后DDL命令效果被回滚;查询表存在
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0065;")
        self.comsh.execut_db_sql("drop TABLE if exists t_eventtrigger_0065;")
        self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0065();")
        msg = self.comsh.execut_db_sql(f"create table t_eventtrigger_0065(id int);")
        self.logger.info(msg)
        self.assertIn("CREATE TABLE", msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0065() RETURNS event_trigger " \
            f"LANGUAGE plpgsql " \
            f"AS \$\$ BEGIN " \
            f"RAISE EXCEPTION 'command % is disabled', tg_tag; " \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertIn("CREATE FUNCTION", msg)

    def test_Index_file_damaged(self):
        self.logger.info("--step1:创建事件触发器")
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER eventtri_eventtrigger_0065 ON ddl_command_end EXECUTE PROCEDURE f_eventtrigger_0065();")
        self.logger.info(msg)
        self.assertEqual(f'CREATE EVENT TRIGGER', msg)
        self.logger.info("--step2:执行DDL删除表")
        msg = self.comsh.execut_db_sql("drop table t_eventtrigger_0065;")
        self.logger.info(msg)
        self.assertEqual(f'ERROR:  command DROP TABLE is disabled', msg)
        self.logger.info("--step3:查询表")
        msg = self.comsh.execut_db_sql("select * from t_eventtrigger_0065;")
        self.logger.info(msg)
        self.assertNotIn(f"ERROR", msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0065;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0065();")
        self.logger.info(msg)
        self.assertIn('DROP FUNCTION', msg)
        # 删除表
        msg = self.comsh.execut_db_sql("drop table if exists t_eventtrigger_0065;")
        self.logger.info(msg)
        self.assertIn('DROP TABLE', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
